package com.asiainfo_linkage.service;


import java.util.Calendar;
import java.util.Date;

import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.Param;
import org.nutz.service.EntityService;

import com.asiainfo_linkage.message.email.EmailInfo;
import com.asiainfo_linkage.message.email.MailSenderInfo;
import com.asiainfo_linkage.message.email.MailSenderInfoBuilder;
import com.asiainfo_linkage.message.email.SimpleMailSender;
import com.asiainfo_linkage.model.RemPlan;
import com.asiainfo_linkage.model.RemTask;
import com.asiainfo_linkage.model.RemTaskMember;

@IocBean	
public class CycleService extends EntityService<RemTask>{

	@Inject
	protected Dao dao;
	@Inject
	protected RemTaskMemberService remTaskMemberService;	
	public void add(@Param("::remPlan.")RemPlan remPlan, @Param("::remTask.")RemTask remTask){
		//存储数据。
		remTask.setRem_type(remTask.getRem_type());
		remTask.setTask_name(remTask.getTask_name());
		remTask.setRem_first_time(remTask.getRem_first_time());
		remTask.setRem_status(remTask.getRem_status());
		remPlan.setPlan_cycle(remPlan.getPlan_cycle());
		remPlan.setRem_cycle_number(remPlan.getRem_cycle_number());
		remTask.setRem_start_date(remTask.getRem_start_date());
		remTask.setRem_end_date(remTask.getRem_end_date());
		remTask.setRem_way(remTask.getRem_way());
		remTask.setRem_content(remTask.getRem_content());
		remTask.setMembers(remTask.getMembers());
		remTask.setRemTaskMembers(remTask.getRemTaskMembers());
		String cycleNumberStr=remPlan.getRem_cycle_number();
		String planCycleStr=remPlan.getPlan_cycle();
		System.out.println(planCycleStr);
		String cycle_type=remPlan.getCycle_type();
		String []cycleNumberArray=cycleNumberStr.split(",");
		String []planCycleArray=planCycleStr.split(",");
		int n=planCycleArray.length;
	    //周期类型：0-按天 数据处理
		if(cycle_type.equals("0")){
			//如果获取周期数量字符串为空，则默认为1
			if(cycleNumberStr.equals(",,")){
				remPlan.setRem_cycle_number("1");				
			}else{
				//如果获取周期数量字符串非空，则取第一个值（已用逗号隔开，存入数组cycleNumberArray[]）
				remPlan.setRem_cycle_number(cycleNumberArray[0]); 
			}				
		}
		 //周期类型：1-按周  数据处理
		else if(cycle_type.equals("1")){
			if(cycleNumberStr.equals(",,")){
				remPlan.setRem_cycle_number("1");			
			}else{
				//如果获取周期数量字符串非空，则取第二个值（已用逗号隔开，存入数组cycleNumberArray[]）
				remPlan.setRem_cycle_number(cycleNumberArray[1]);
			}			
		}
		//周期类型：2-按月  数据处理
		else if(cycle_type.equals("2")){
			if(cycleNumberStr.equals(",,")){
				remPlan.setRem_cycle_number("1");			
			}else{
				//如果获取周期数量字符串非空，则取第三个值（已用逗号隔开，存入数组cycleNumberArray[]）
				remPlan.setRem_cycle_number(cycleNumberArray[2]);
			}
		}	
		this.dao().insert(remTask);
		if(remTask.getRemTaskMembers()!=null && remTask.getRemTaskMembers().size()>0){
			for(RemTaskMember remTaskMember:remTask.getRemTaskMembers()){
				remTaskMember.setTask_id(remTask.getTask_id());
				remTaskMemberService.dao().insert(remTaskMember);
			}
			
		}
		remPlan.setTask_id(remTask.getTask_id());
		//周期类型：0-按天 数据处理
		if(cycle_type.equals("0")){
			remPlan.setPlan_cycle(planCycleArray[0]);
			//计划周期：01 每工作日（11-15）
			if(remPlan.getPlan_cycle().equals("01")){
			String []pcArray=new String[5];
			pcArray[0]="11";
			pcArray[1]="12";
			pcArray[2]="13";
			pcArray[3]="14";
			pcArray[4]="15";
			for(int j=0;j<5;j++){
				remPlan.setPlan_cycle(pcArray[j]);
				this.dao().insert(remPlan);
				}	
			}
			//计划周期：00 每天
			else{
			this.dao().insert(remPlan);
			}			
		}
		//周期类型：1-按周 数据处理
		else if(cycle_type.equals("1")){
			for(int i=1;i<n-1;i++){
				/*Sql sql = Sqls.create("SELECT MAX(TASK_ID) FROM REM_TASK$condition");
				dao.execute(sql);
				Long m=(long) sql.getInt();
			    remPlan.setTask_id(m);*/
				remPlan.setPlan_cycle(planCycleArray[i]);
				//获取nexttime
				Date ft=remTask.getRem_first_time();
				Calendar cal=Calendar.getInstance();
				cal.setTime(ft);
				int _week = cal.get(Calendar.DAY_OF_WEEK)-1;//获取周几
				
				if(remPlan.getPlan_cycle().equals("11")){//firstdate是周一
			    	   if(_week==1){
						int m=0;
						cal.add(Calendar.DATE, m);
						remPlan.setNexttime(cal.getTime());
						this.dao().insert(remPlan);
						}else if(_week==2){
							int m=6;
							cal.add(Calendar.DATE, m);
							remPlan.setNexttime(cal.getTime());
							this.dao().insert(remPlan);
						}else if(_week==3){
							int m=5;
							cal.add(Calendar.DATE, m);
							remPlan.setNexttime(cal.getTime());
							this.dao().insert(remPlan);
						}else if(_week==4){
							int m=4;
							cal.add(Calendar.DATE, m);
							remPlan.setNexttime(cal.getTime());
							this.dao().insert(remPlan);
						}else if(_week==5){
							int m=3;
							cal.add(Calendar.DATE, m);
							remPlan.setNexttime(cal.getTime());
							this.dao().insert(remPlan);
						}else if(_week==6){
							int m=2;
							cal.add(Calendar.DATE, m);
							remPlan.setNexttime(cal.getTime());
							this.dao().insert(remPlan);
						}else if(_week==0){
							int m=1;
							cal.add(Calendar.DATE, m);
							remPlan.setNexttime(cal.getTime());
							this.dao().insert(remPlan);
						}
					}else if(remPlan.getPlan_cycle().equals("12")){//firstdate是周二
							if(_week==1){
							int m=1;
							cal.add(Calendar.DATE, m);
							remPlan.setNexttime(cal.getTime());
							this.dao().insert(remPlan);
							}else if(_week==2){
								int m=0;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}else if(_week==3){
								int m=6;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}else if(_week==4){
								int m=5;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}else if(_week==5){
								int m=4;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}else if(_week==6){
								int m=3;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}else if(_week==0){
								int m=2;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}
					}else if(remPlan.getPlan_cycle().equals("13")){//firstdate是周三
							if(_week==1){
							int m=2;
							cal.add(Calendar.DATE, m);
							remPlan.setNexttime(cal.getTime());
							this.dao().insert(remPlan);
							}else if(_week==2){
								int m=1;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}else if(_week==3){
								int m=0;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}else if(_week==4){
								int m=6;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}else if(_week==5){
								int m=5;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}else if(_week==6){
								int m=4;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}else if(_week==0){
								int m=3;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}
					}else if(remPlan.getPlan_cycle().equals("14")){//firstdate是周四
						if(_week==1){
							int m=3;
							cal.add(Calendar.DATE, m);
							remPlan.setNexttime(cal.getTime());
							this.dao().insert(remPlan);
							}else if(_week==2){
								int m=2;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}else if(_week==3){
								int m=1;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}else if(_week==4){
								int m=0;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}else if(_week==5){
								int m=6;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}else if(_week==6){
								int m=5;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}else if(_week==0){
								int m=4;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}
					}else if(remPlan.getPlan_cycle().equals("15")){//firstdate是周五
						if(_week==1){
							int m=4;
							cal.add(Calendar.DATE, m);
							remPlan.setNexttime(cal.getTime());
							this.dao().insert(remPlan);
							}else if(_week==2){
								int m=3;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}else if(_week==3){
								int m=2;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}else if(_week==4){
								int m=1;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}else if(_week==5){
								int m=0;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}else if(_week==6){
								int m=6;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}else if(_week==0){
								int m=5;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}
					}else if(remPlan.getPlan_cycle().equals("16")){//firstdate是周六
							if(_week==1){
							int m=5;
							cal.add(Calendar.DATE, m);
							remPlan.setNexttime(cal.getTime());
							this.dao().insert(remPlan);
							}else if(_week==2){
								int m=4;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}else if(_week==3){
								int m=3;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}else if(_week==4){
								int m=2;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}else if(_week==5){
								int m=1;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}else if(_week==6){
								int m=0;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}else if(_week==0){
								int m=6;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}
					}else if(remPlan.getPlan_cycle().equals("17")){//firstdate是周日
							if(_week==1){
								int m=6;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}else if(_week==2){
								int m=5;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}else if(_week==3){
								int m=4;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}else if(_week==4){
								int m=3;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}else if(_week==5){
								int m=2;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}else if(_week==6){
								int m=1;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}else if(_week==0){
								int m=0;
								cal.add(Calendar.DATE, m);
								remPlan.setNexttime(cal.getTime());
								this.dao().insert(remPlan);
							}
					}
				
			}
		}
		//周期类型：2-按月  数据处理
		else if(cycle_type.equals("2")){
			remPlan.setPlan_cycle(planCycleArray[n-1]);
			this.dao().insert(remPlan);
		}	
		System.out.println("操作完成");
		
	}	
}
